
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>异步编程 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="异步编程之Future.html" />
    
    
    <link rel="prev" href="库与导入.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >异步编程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#dart-&#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;"><b></b>Dart &#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#dart-&#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;"><b></b>Dart &#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;</a></li><li><span class="title-icon "></span><a href="#&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;"><b></b>&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;</a></li><li><span class="title-icon "></span><a href="#&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;"><b></b>&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="dart-&#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;"><a name="dart-&#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#dart-&#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>Dart &#x7684;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;</h2>
<p>&#x7F16;&#x7A0B;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x6267;&#x884C;&#xFF0C;&#x901A;&#x5E38;&#x5206;&#x4E3A;<code>&#x540C;&#x6B65;</code>&#x4E0E;<code>&#x5F02;&#x6B65;</code>&#x4E24;&#x79CD;&#x3002;&#x7B80;&#x5355;&#x8BF4;&#xFF0C;&#x540C;&#x6B65;&#x5C31;&#x662F;&#x6309;&#x7167;&#x4EE3;&#x7801;&#x7684;&#x7F16;&#x5199;&#x987A;&#x5E8F;&#xFF0C;&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x4F9D;&#x6B21;&#x6267;&#x884C;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x6211;&#x4EEC;&#x6700;&#x5E38;&#x63A5;&#x89E6;&#x7684;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;&#x3002;&#x4F46;&#x662F;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x7684;&#x7F3A;&#x70B9;&#x4E5F;&#x663E;&#x800C;&#x6613;&#x89C1;&#xFF0C;&#x5982;&#x679C;&#x5176;&#x4E2D;&#x67D0;&#x4E00;&#x884C;&#x6216;&#x51E0;&#x884C;&#x4EE3;&#x7801;&#x975E;&#x5E38;&#x8017;&#x65F6;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x4F7F;&#x5F97;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E0D;&#x80FD;&#x88AB;&#x7ACB;&#x523B;&#x6267;&#x884C;&#x3002;</p>
<p>&#x5F02;&#x6B65;&#x7684;&#x51FA;&#x73B0;&#x6B63;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x79CD;&#x95EE;&#x9898;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x4F7F;&#x67D0;&#x90E8;&#x5206;&#x8017;&#x65F6;&#x4EE3;&#x7801;&#x4E0D;&#x5728;&#x5F53;&#x524D;&#x8FD9;&#x6761;&#x6267;&#x884C;&#x7EBF;&#x8DEF;&#x4E0A;&#x7ACB;&#x523B;&#x6267;&#x884C;&#xFF0C;&#x90A3;&#x7A76;&#x7ADF;&#x600E;&#x4E48;&#x6267;&#x884C;&#x5462;&#xFF1F;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x6848;&#x662F;&#x4F7F;&#x7528;&#x591A;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E5F;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x5F00;&#x8F9F;&#x53E6;&#x4E00;&#x6761;&#x6267;&#x884C;&#x7EBF;&#xFF0C;&#x7136;&#x540E;&#x8BA9;&#x8017;&#x65F6;&#x4EE3;&#x7801;&#x5728;&#x53E6;&#x4E00;&#x6761;&#x6267;&#x884C;&#x7EBF;&#x4E0A;&#x8FD0;&#x884C;&#xFF0C;&#x8FD9;&#x6837;&#x4E24;&#x6761;&#x6267;&#x884C;&#x7EBF;&#x5E76;&#x5217;&#xFF0C;&#x8017;&#x65F6;&#x4EE3;&#x7801;&#x81EA;&#x7136;&#x4E5F;&#x5C31;&#x4E0D;&#x80FD;&#x963B;&#x585E;&#x4E3B;&#x6267;&#x884C;&#x7EBF;&#x4E0A;&#x7684;&#x4EE3;&#x7801;&#x4E86;&#x3002;</p>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x867D;&#x7136;&#x597D;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x5927;&#x91CF;&#x5E76;&#x53D1;&#x65F6;&#xFF0C;&#x4ECD;&#x7136;&#x5B58;&#x5728;&#x4E24;&#x4E2A;&#x8F83;&#x5927;&#x7684;&#x7F3A;&#x9677;&#xFF0C;&#x4E00;&#x4E2A;&#x662F;&#x5F00;&#x8F9F;&#x7EBF;&#x7A0B;&#x6BD4;&#x8F83;&#x8017;&#x8D39;&#x8D44;&#x6E90;&#xFF0C;&#x7EBF;&#x7A0B;&#x5F00;&#x591A;&#x4E86;&#x673A;&#x5668;&#x5403;&#x4E0D;&#x6D88;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x5219;&#x662F;&#x7EBF;&#x7A0B;&#x7684;&#x9501;&#x95EE;&#x9898;&#xFF0C;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x64CD;&#x4F5C;&#x5171;&#x4EAB;&#x5185;&#x5B58;&#x65F6;&#x9700;&#x8981;&#x52A0;&#x9501;&#xFF0C;&#x590D;&#x6742;&#x60C5;&#x51B5;&#x4E0B;&#x7684;&#x9501;&#x7ADE;&#x4E89;&#x4E0D;&#x4EC5;&#x4F1A;&#x964D;&#x4F4E;&#x6027;&#x80FD;&#xFF0C;&#x8FD8;&#x53EF;&#x80FD;&#x9020;&#x6210;&#x6B7B;&#x9501;&#x3002;&#x56E0;&#x6B64;&#x53C8;&#x51FA;&#x73B0;&#x4E86;&#x57FA;&#x4E8E;&#x4E8B;&#x4EF6;&#x7684;&#x5F02;&#x6B65;&#x6A21;&#x578B;&#x3002;&#x7B80;&#x5355;&#x8BF4;&#x5C31;&#x662F;&#x5728;&#x67D0;&#x4E2A;&#x5355;&#x7EBF;&#x7A0B;&#x4E2D;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x548C;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x961F;&#x5217;&#xFF0C;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E0D;&#x65AD;&#x7684;&#x4ECE;&#x4E8B;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x53D6;&#x51FA;&#x4E8B;&#x4EF6;&#x6765;&#x6267;&#x884C;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x4E8B;&#x4EF6;&#x5C31;&#x597D;&#x6BD4;&#x662F;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x6BCF;&#x5F53;&#x9047;&#x5230;&#x8017;&#x65F6;&#x7684;&#x4E8B;&#x4EF6;&#x65F6;&#xFF0C;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E0D;&#x4F1A;&#x505C;&#x4E0B;&#x6765;&#x7B49;&#x5F85;&#x7ED3;&#x679C;&#xFF0C;&#x5B83;&#x4F1A;&#x8DF3;&#x8FC7;&#x8017;&#x65F6;&#x4E8B;&#x4EF6;&#xFF0C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5176;&#x540E;&#x7684;&#x4E8B;&#x4EF6;&#x3002;&#x5F53;&#x4E0D;&#x8017;&#x65F6;&#x7684;&#x4E8B;&#x4EF6;&#x90FD;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x518D;&#x6765;&#x67E5;&#x770B;&#x8017;&#x65F6;&#x4E8B;&#x4EF6;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x8017;&#x65F6;&#x4E8B;&#x4EF6;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x6574;&#x4E2A;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#xFF0C;&#x8FD9;&#x8BA9;&#x5B83;&#x540E;&#x9762;&#x7684;&#x4E8B;&#x4EF6;&#x4E5F;&#x4F1A;&#x6709;&#x673A;&#x4F1A;&#x5F97;&#x5230;&#x6267;&#x884C;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5F88;&#x5BB9;&#x6613;&#x53D1;&#x73B0;&#xFF0C;&#x8FD9;&#x79CD;&#x57FA;&#x4E8E;&#x4E8B;&#x4EF6;&#x7684;&#x5F02;&#x6B65;&#x6A21;&#x578B;&#xFF0C;&#x53EA;&#x9002;&#x5408;<code>I/O</code>&#x5BC6;&#x96C6;&#x578B;&#x7684;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#xFF0C;&#x56E0;&#x4E3A;<code>I/O</code>&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#xFF0C;&#x5F80;&#x5F80;&#x662F;&#x628A;&#x65F6;&#x95F4;&#x6D6A;&#x8D39;&#x5728;&#x7B49;&#x5F85;&#x5BF9;&#x65B9;&#x4F20;&#x9001;&#x6570;&#x636E;&#x6216;&#x8005;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x79CD;&#x5F02;&#x6B65;&#x6A21;&#x578B;&#x5F80;&#x5F80;&#x7528;&#x4E8E;&#x7F51;&#x7EDC;&#x670D;&#x52A1;&#x5668;&#x5E76;&#x53D1;&#x3002;&#x5982;&#x679C;&#x662F;&#x8BA1;&#x7B97;&#x5BC6;&#x96C6;&#x578B;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5219;&#x5E94;&#x5F53;&#x5C3D;&#x53EF;&#x80FD;&#x5229;&#x7528;&#x5904;&#x7406;&#x5668;&#x7684;&#x591A;&#x6838;&#xFF0C;&#x5B9E;&#x73B0;&#x5E76;&#x884C;&#x8BA1;&#x7B97;&#x3002;</p>
<p><img src="https://img-blog.csdnimg.cn/20190505100632250.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9hcmN0aWNmb3guYmxvZy5jc2RuLm5ldA==,size_16,color_FFFFFF,t_70" alt="&#x5728;&#x8FD9;&#x91CC;&#x63D2;&#x5165;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
<h2 id="dart-&#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;"><a name="dart-&#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;" class="anchor-navigation-ex-anchor" href="#dart-&#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;"><i class="fa fa-link" aria-hidden="true"></i></a>Dart &#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;</h2>
<p>Dart &#x662F;&#x4E8B;&#x4EF6;&#x9A71;&#x52A8;&#x7684;&#x4F53;&#x7CFB;&#x7ED3;&#x6784;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x57FA;&#x4E8E;&#x5177;&#x6709;&#x5355;&#x4E2A;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x548C;&#x4E24;&#x4E2A;&#x961F;&#x5217;&#x7684;&#x5355;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x6A21;&#x578B;&#x3002; Dart&#x867D;&#x7136;&#x63D0;&#x4F9B;&#x8C03;&#x7528;&#x5806;&#x6808;&#x3002; &#x4F46;&#x662F;&#x5B83;&#x4F7F;&#x7528;&#x4E8B;&#x4EF6;&#x5728;&#x751F;&#x4EA7;&#x8005;&#x548C;&#x6D88;&#x8D39;&#x8005;&#x4E4B;&#x95F4;&#x4F20;&#x8F93;&#x4E0A;&#x4E0B;&#x6587;&#x3002; &#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x7531;&#x5355;&#x4E2A;&#x7EBF;&#x7A0B;&#x652F;&#x6301;&#xFF0C;&#x56E0;&#x6B64;&#x6839;&#x672C;&#x4E0D;&#x9700;&#x8981;&#x540C;&#x6B65;&#x548C;&#x9501;&#x5B9A;&#x3002;</p>
<p>Dart &#x7684;&#x4E24;&#x4E2A;&#x961F;&#x5217;&#x5206;&#x522B;&#x662F;</p>
<ul>
<li><p><code>MicroTask queue</code>  &#x5FAE;&#x4EFB;&#x52A1;&#x961F;&#x5217;</p>
</li>
<li><p><code>Event queue</code> &#x4E8B;&#x4EF6;&#x961F;&#x5217;</p>
</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20190505100705326.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9hcmN0aWNmb3guYmxvZy5jc2RuLm5ldA==,size_16,color_FFFFFF,t_70" alt="&#x5728;&#x8FD9;&#x91CC;&#x63D2;&#x5165;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
<p>Dart&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x5982;&#x4E0A;&#x56FE;&#x6240;&#x793A;</p>
<ol>
<li>&#x5148;&#x67E5;&#x770B;<code>MicroTask</code>&#x961F;&#x5217;&#x662F;&#x5426;&#x4E3A;&#x7A7A;&#xFF0C;&#x4E0D;&#x662F;&#x5219;&#x5148;&#x6267;&#x884C;<code>MicroTask</code>&#x961F;&#x5217;</li>
<li>&#x4E00;&#x4E2A;<code>MicroTask</code>&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;&#x68C0;&#x67E5;&#x6709;&#x6CA1;&#x6709;&#x4E0B;&#x4E00;&#x4E2A;<code>MicroTask</code>&#xFF0C;&#x76F4;&#x5230;<code>MicroTask</code>&#x961F;&#x5217;&#x4E3A;&#x7A7A;&#xFF0C;&#x624D;&#x53BB;&#x6267;&#x884C;<code>Event</code>&#x961F;&#x5217;</li>
<li>&#x5728;<code>Evnet</code> &#x961F;&#x5217;&#x53D6;&#x51FA;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x5B8C;&#x540E;&#xFF0C;&#x518D;&#x6B21;&#x8FD4;&#x56DE;&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x53BB;&#x68C0;&#x67E5;<code>MicroTask</code>&#x961F;&#x5217;&#x662F;&#x5426;&#x4E3A;&#x7A7A;</li>
</ol>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x5C06;&#x4EFB;&#x52A1;&#x52A0;&#x5165;&#x5230;<code>MicroTask</code>&#x4E2D;&#x53EF;&#x4EE5;&#x88AB;&#x5C3D;&#x5FEB;&#x6267;&#x884C;&#xFF0C;&#x4F46;&#x4E5F;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x5F53;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x5728;&#x5904;&#x7406;<code>MicroTask</code>&#x961F;&#x5217;&#x65F6;&#xFF0C;<code>Event</code>&#x961F;&#x5217;&#x4F1A;&#x88AB;&#x5361;&#x4F4F;&#xFF0C;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x65E0;&#x6CD5;&#x5904;&#x7406;&#x9F20;&#x6807;&#x5355;&#x51FB;&#x3001;I/O&#x6D88;&#x606F;&#x7B49;&#x7B49;&#x4E8B;&#x4EF6;&#x3002;</p>
<h2 id="&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;"><a name="&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;" class="anchor-navigation-ex-anchor" href="#&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;</h2>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x4EE5;&#x4E0B;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x90FD;&#x5B9A;&#x4E49;&#x5728;<code>dart:async</code>&#x5E93;&#x4E2D;&#x3002;</p>
<p><strong>&#x5C06;&#x4EFB;&#x52A1;&#x6DFB;&#x52A0;&#x5230;<code>MicroTask</code>&#x961F;&#x5217;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span>  myTask(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;this is my task&quot;</span>);
}

<span class="hljs-keyword">void</span>  main() {
    <span class="hljs-comment">// 1. &#x4F7F;&#x7528; scheduleMicrotask &#x65B9;&#x6CD5;&#x6DFB;&#x52A0;</span>
    scheduleMicrotask(myTask);

    <span class="hljs-comment">// 2. &#x4F7F;&#x7528;Future&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;</span>
    <span class="hljs-keyword">new</span>  Future.microtask(myTask);
}
</code></pre>
<p><strong>&#x5C06;&#x4EFB;&#x52A1;&#x6DFB;&#x52A0;&#x5230;<code>Event</code>&#x961F;&#x5217;</strong></p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span>  myTask(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;this is my task&quot;</span>);
}

<span class="hljs-keyword">void</span>  main() {
    <span class="hljs-keyword">new</span>  Future(myTask);
}
</code></pre>
<p>&#x73B0;&#x5728;&#x5B66;&#x4F1A;&#x4E86;&#x8C03;&#x5EA6;&#x4EFB;&#x52A1;&#xFF0C;&#x8D76;&#x7D27;&#x7F16;&#x5199;&#x4EE3;&#x7801;&#x9A8C;&#x8BC1;&#x4EE5;&#x4E0A;&#x7684;&#x7ED3;&#x8BBA;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span>  main() {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);

    <span class="hljs-keyword">new</span>  Future((){
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;this is my task&quot;</span>);
    });

    <span class="hljs-keyword">new</span>  Future.microtask((){
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;this is microtask&quot;</span>);
    });

    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main stop&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
main stop
this is microtask
this is my task
</code></pre><p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x4EE3;&#x7801;&#x7684;&#x8FD0;&#x884C;&#x987A;&#x5E8F;&#x5E76;&#x4E0D;&#x662F;&#x6309;&#x7167;&#x6211;&#x4EEC;&#x7684;&#x7F16;&#x5199;&#x987A;&#x5E8F;&#x6765;&#x7684;&#xFF0C;&#x5C06;&#x4EFB;&#x52A1;&#x6DFB;&#x52A0;&#x5230;&#x961F;&#x5217;&#x5E76;&#x4E0D;&#x7B49;&#x4E8E;&#x7ACB;&#x523B;&#x6267;&#x884C;&#xFF0C;&#x5B83;&#x4EEC;&#x662F;&#x5F02;&#x6B65;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x5F53;&#x524D;<code>main</code>&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x5B8C;&#x4E4B;&#x540E;&#xFF0C;&#x624D;&#x4F1A;&#x53BB;&#x6267;&#x884C;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x4E14;<code>MicroTask</code>&#x961F;&#x5217;&#x8FD0;&#x884C;&#x5728;<code>Event</code>&#x961F;&#x5217;&#x4E4B;&#x524D;&#x3002;</p>
<h2 id="&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;"><a name="&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;" class="anchor-navigation-ex-anchor" href="#&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5EF6;&#x65F6;&#x4EFB;&#x52A1;</h2>
<p>&#x5982;&#x9700;&#x8981;&#x5C06;&#x4EFB;&#x52A1;&#x5EF6;&#x4F38;&#x6267;&#x884C;&#xFF0C;&#x5219;&#x53EF;&#x4F7F;&#x7528;<code>Future.delayed</code>&#x65B9;&#x6CD5;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">new</span>  Future.delayed(<span class="hljs-keyword">new</span>  <span class="hljs-built_in">Duration</span>(seconds:<span class="hljs-number">1</span>),(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;task delayed&apos;</span>);
});
</code></pre>
<p>&#x8868;&#x793A;&#x5728;&#x5EF6;&#x8FDF;&#x65F6;&#x95F4;&#x5230;&#x4E86;&#x4E4B;&#x540E;&#x5C06;&#x4EFB;&#x52A1;&#x52A0;&#x5165;&#x5230;<code>Event</code>&#x961F;&#x5217;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x51C6;&#x786E;&#x7684;&#xFF0C;&#x4E07;&#x4E00;&#x524D;&#x9762;&#x6709;&#x5F88;&#x8017;&#x65F6;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x7684;&#x5EF6;&#x8FDF;&#x4EFB;&#x52A1;&#x4E0D;&#x4E00;&#x5B9A;&#x80FD;&#x51C6;&#x65F6;&#x8FD0;&#x884C;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:async&apos;</span>;
<span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span>  main() {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);

    <span class="hljs-keyword">new</span> Future.delayed(<span class="hljs-keyword">new</span>  <span class="hljs-built_in">Duration</span>(seconds:<span class="hljs-number">1</span>),(){
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;task delayed&apos;</span>);
    });

    <span class="hljs-keyword">new</span> Future((){
        <span class="hljs-comment">// &#x6A21;&#x62DF;&#x8017;&#x65F6;5&#x79D2;</span>
        sleep(<span class="hljs-built_in">Duration</span>(seconds:<span class="hljs-number">5</span>));
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;5s task&quot;</span>);
    });

    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main stop&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
main stop
5s task
task delayed
</code></pre><p>&#x4ECE;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;<code>delayed</code>&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x5728;&#x524D;&#x9762;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x663E;&#x7136;&#x5E76;&#x672A;&#x76F4;&#x63A5;&#x5C06;&#x4EFB;&#x52A1;&#x52A0;&#x5165;<code>Event</code>&#x961F;&#x5217;&#xFF0C;&#x800C;&#x662F;&#x9700;&#x8981;&#x7B49;&#x5F85;1&#x79D2;&#x4E4B;&#x540E;&#x624D;&#x4F1A;&#x53BB;&#x5C06;&#x4EFB;&#x52A1;&#x52A0;&#x5165;&#xFF0C;&#x4F46;&#x5728;&#x8FD9;1&#x79D2;&#x4E4B;&#x95F4;&#xFF0C;&#x540E;&#x9762;&#x7684;<code>new  Future</code>&#x4EE3;&#x7801;&#x76F4;&#x63A5;&#x5C06;&#x4E00;&#x4E2A;&#x8017;&#x65F6;&#x4EFB;&#x52A1;&#x52A0;&#x5165;&#x5230;&#x4E86;<code>Event</code>&#x961F;&#x5217;&#xFF0C;&#x8FD9;&#x5C31;&#x76F4;&#x63A5;&#x5BFC;&#x81F4;&#x5199;&#x5728;&#x524D;&#x9762;&#x7684;<code>delayed</code>&#x4EFB;&#x52A1;&#x5728;1&#x79D2;&#x540E;&#x53EA;&#x80FD;&#x88AB;&#x52A0;&#x5165;&#x5230;&#x8017;&#x65F6;&#x4EFB;&#x52A1;&#x4E4B;&#x540E;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x524D;&#x9762;&#x8017;&#x65F6;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x5B83;&#x624D;&#x6709;&#x673A;&#x4F1A;&#x5F97;&#x5230;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x79CD;&#x673A;&#x5236;&#x4F7F;&#x5F97;&#x5EF6;&#x8FDF;&#x4EFB;&#x52A1;&#x53D8;&#x5F97;&#x4E0D;&#x592A;&#x53EF;&#x9760;&#xFF0C;&#x4F60;&#x65E0;&#x6CD5;&#x786E;&#x5B9A;&#x5EF6;&#x8FDF;&#x4EFB;&#x52A1;&#x5230;&#x5E95;&#x5728;&#x5EF6;&#x8FDF;&#x591A;&#x4E45;&#x4E4B;&#x540E;&#x88AB;&#x6267;&#x884C;&#x3002;</p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:34:23
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="库与导入.html" class="navigation navigation-prev " aria-label="Previous page: 库与导入">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="异步编程之Future.html" class="navigation navigation-next " aria-label="Next page: Future">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"异步编程","level":"1.3.5","depth":2,"next":{"title":"Future","level":"1.3.5.1","depth":3,"path":"异步编程之Future.md","ref":"异步编程之Future.md","articles":[]},"previous":{"title":"库与导入","level":"1.3.4","depth":2,"path":"库与导入.md","ref":"库与导入.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"异步编程.md","mtime":"2021-03-18T05:34:23.926Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

